AddPrefabPostInit(
    "reskin_tool",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end
        


        inst.components.spellcaster.can_cast_fn__old_npc = inst.components.spellcaster.can_cast_fn
        inst.components.spellcaster.can_cast_fn = function(doer, target, pos)
            if target and target:HasTag("npc_item_skin") then
                return true
            end
            return inst.components.spellcaster.can_cast_fn__old_npc(doer,target,pos)
        end


        inst.components.spellcaster.spell__old_npc = inst.components.spellcaster.spell
        inst.components.spellcaster.spell = function(tool, target, pos, caster)
            -- if target and target.GetSkinBuild then
            --     print("GetSkinBuild",target:GetSkinBuild())
            -- end
            if target and target:HasTag("npc_item_skin") then
                target:PushEvent("npc_reskin",{doer = caster})
                local color = {
                    Vector3(255,0,0),
                    Vector3(0,255,0),
                    Vector3(0,0,255),
                    Vector3(255,255,0),
                    Vector3(255,0,255),
                    Vector3(0,255,255),
                }
                local pt = Vector3(target.Transform:GetWorldPosition())
                SpawnPrefab("npc_fx_knowledge_flash"):PushEvent("Set",{
                    pt = Vector3(pt.x,-1,pt.z),
                    color = color[math.random(#color)],
                    sound = "terraria1/skins/spectrepaintbrush"
                })
                return
            end
            inst.components.spellcaster.spell__old_npc(tool, target, pos, caster)
        end
        
    end
)